package com.ymttest.business.service;

import com.google.gson.JsonObject;
import com.ymatou.iapi.im.parameter.*;
import com.ymatou.iapi.im.parameter.emu.MessageIsNewType;
import com.ymatou.iapi.im.service.*;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import org.apache.http.client.ClientProtocolException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ImCallService {

	/**
	 * 客服取消标记重要会话
	 * @param userId
	 * @param toUserId
	 * @throws IOException
	 */
	public static void CancelSetMessageSessionImportant(int userId,int toUserId) throws IOException {
		CancelSetMessageSessionImportantBean cancelSetMessageSessionImportantBean = new CancelSetMessageSessionImportantBean();
		cancelSetMessageSessionImportantBean.setUserId(userId);
		cancelSetMessageSessionImportantBean.setToUserId(toUserId);
		CancelSetMessageSessionImportantCall cancelSetMessageSessionImportantCall = new CancelSetMessageSessionImportantCall();
		cancelSetMessageSessionImportantCall.setData(cancelSetMessageSessionImportantBean);
		cancelSetMessageSessionImportantCall.callService();
	}

	/**
	 * 客服标记重要会话
	 * @param userId 客服id
	 * @param otherUserId 对方用户id
	 * @throws IOException
	 */
	public static void SetMessageSessionImportant(int userId,int otherUserId) throws IOException {
		SetMessageSessionImportantBean setMessageSessionImportantBean = new SetMessageSessionImportantBean();
		setMessageSessionImportantBean.setUserId(userId);
		setMessageSessionImportantBean.setToUserId(otherUserId);
		SetMessageSessionImportantCall setMessageSessionImportantCall = new SetMessageSessionImportantCall();
		setMessageSessionImportantCall.setData(setMessageSessionImportantBean);
		setMessageSessionImportantCall.callService();
	}

	/**
	 * 买手删除会话备注
	 * @param fromUserId 买手id
	 * @param toUserId 用户id
	 * @throws IOException
	 */
	public static void DeleteSessionRemark(int fromUserId,int toUserId) throws IOException {
		DeleteSessionRemarkBean deleteSessionRemarkBean = new DeleteSessionRemarkBean();
		deleteSessionRemarkBean.setFromUserId(fromUserId);
		deleteSessionRemarkBean.setToUserId(toUserId);
		DeleteSessionRemarkCall deleteSessionRemarkCall = new DeleteSessionRemarkCall();
		deleteSessionRemarkCall.setData(deleteSessionRemarkBean);
		deleteSessionRemarkCall.callService();
	}

	/**
	 * 设置会话免打扰
	 * @param fromUserId 用户id
	 * @param toUserId 对方用户id
	 * @throws IOException
	 */
	public static void SetSessionSilent(int fromUserId,int toUserId) throws IOException {
		SetSessionSilentBean setSessionSilentBean = new SetSessionSilentBean();
		setSessionSilentBean.setFromUserId(fromUserId);
		setSessionSilentBean.setToUserId(toUserId);
		SetSessionSilentCall setSessionSilentCall = new SetSessionSilentCall();
		setSessionSilentCall.setData(setSessionSilentBean);
		setSessionSilentCall.callService();
	}


	/**
	 * 取消会话免打扰
	 * @param fromUserId
	 * @param toUserId
	 * @throws IOException
	 */
	public void Cancelsetsessionsilent(int fromUserId,int toUserId) throws IOException {
		CancelsetsessionsilentBean cancelsetsessionsilentBean = new CancelsetsessionsilentBean();
		cancelsetsessionsilentBean.setToUserId(toUserId);
		cancelsetsessionsilentBean.setFromUserId(fromUserId);
		CancelsetsessionsilentCall cancelsetsessionsilentCall = new CancelsetsessionsilentCall();
		cancelsetsessionsilentCall.setData(cancelsetsessionsilentBean);
		cancelsetsessionsilentCall.callService();
	}

	/**
	 * 买手新增会话备注
	 * @param fromUserId 买手id
	 * @param toUserId 用户id
	 * @param remark 备注
	 * @throws IOException
	 */
	public static void SendSessionRemark(int fromUserId,int toUserId,String remark) throws IOException {
		SendSessionRemarkBean sendSessionRemarkBean = new SendSessionRemarkBean();
		sendSessionRemarkBean.setRemark(remark);
		sendSessionRemarkBean.setToUserId(toUserId);
		sendSessionRemarkBean.setFromUserId(fromUserId);
		SendSessionRemarkCall sendSessionRemarkCall = new SendSessionRemarkCall();
		sendSessionRemarkCall.setData(sendSessionRemarkBean);
		sendSessionRemarkCall.callService();
	}

	/**
	 * 初始化码头洋洋发送消息
	 * @param CsId 客服id
	 * @param UserId 接收方id
	 * @param CsName 接收方名称
	 * @return
	 */
	public static SendMessageBean initSendMessage(Integer CsId,Integer UserId,String CsName){
		SendMessageBean sendMessageBean = new SendMessageBean();
		sendMessageBean.setMessage("Hello,world");
		sendMessageBean.setIsRiskyUser(false);
		sendMessageBean.setAutoSetRead(true);
		sendMessageBean.setSessionNumber(CsId+"_"+UserId);
		sendMessageBean.setCSName(CsName);
		sendMessageBean.setFromUserId(CsId);
		sendMessageBean.setToUserId(UserId);
		return sendMessageBean;
	}

	/**
	 * 码头洋洋发送消息
	 * @param sendMessageBean
	 */
	public static JsonObject SendMessage(SendMessageBean sendMessageBean) throws IOException {
		SendMessageCall sendMessageCall = new SendMessageCall();
		sendMessageCall.setData(sendMessageBean);
		sendMessageCall.callService();
		JsonObject jet = sendMessageCall.getJsonObject().get("Result").getAsJsonObject();
		return jet;
	}

	/**
	 * 买家给买手发消息bean
	 * @param FromUserId 买家id
	 * @param msg
	 */
	public static SendMessageToSellerBean initSendMessageToSellerBean(Integer FromUserId,Integer ToUserId,String msg) throws IOException {
		SendMessageToSellerBean sendbean=new SendMessageToSellerBean();
		sendbean.setAppId("app.ymatou.com");
		sendbean.setFromUserId(FromUserId);
		sendbean.setToUserId(ToUserId);
		sendbean.setMessage(msg);
		sendbean.setIsAutoReply(false);
		sendbean.setSessionNumber(FromUserId+"_"+ToUserId);
		sendbean.setCSId(ToUserId);
		sendbean.setCSName("wesper");
		return sendbean;
	}
	/**
	 * 买家给买手发消息
	 * @param SendMessageToSellerBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JsonObject SendMessageToSeller(SendMessageToSellerBean SendMessageToSellerBean) throws ClientProtocolException, IOException{
		SendMessageToSellerCall SendMessageToSellerCall=new SendMessageToSellerCall();
		SendMessageToSellerCall.setData(SendMessageToSellerBean);
		SendMessageToSellerCall.callService();
		JsonObject jet = SendMessageToSellerCall.getJsonObject().get("Result").getAsJsonObject();
		return jet;
	}
	
	/**
	 * 买手给买家发送消息
	 * @param FromUserId 买手id
	 * @param ToUserId 买家id
	 * @param msg
	 */
	public static SendMessageToBuyerBean initSendMessageToBuyerBean(Integer SellerId,Integer ToUserId,String msg) throws IOException {
		SendMessageToBuyerBean sendbean=new SendMessageToBuyerBean();
		sendbean.setAppId("app.ymatou.com");
		sendbean.setFromUserId(SellerId);
		sendbean.setToUserId(ToUserId);
		sendbean.setMessage(msg);
		sendbean.setSessionNumber(SellerId+"_"+ToUserId);
		sendbean.setCSId(SellerId);
		sendbean.setCSName(new JUserWapper().selectUserinfoByUid(SellerId).get(0).getLoginId());
		return sendbean;
	}
	/**
	 * 发消息
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JsonObject SendMessageToBuyer(SendMessageToBuyerBean SendMessageToBuyerBean) throws ClientProtocolException, IOException{
		SendMessageToBuyerCall SendMessageToBuyerCall=new SendMessageToBuyerCall();
		SendMessageToBuyerCall.setData(false,SendMessageToBuyerBean);
		SendMessageToBuyerCall.callService();
		JsonObject jet = SendMessageToBuyerCall.getJsonObject().get("Result").getAsJsonObject();
		return jet;
	}

	public static SendCommentBean initSendComment(){
		SendCommentBean sendCommentBean = new SendCommentBean();
		int SenderUserId = 3791823;
		sendCommentBean.setSenderUserId(SenderUserId);
		int ToUserId = 20354944;
		sendCommentBean.setToUserId(ToUserId);
		sendCommentBean.setMessage("{\"Content\":\"意大利原装进口Marvis爱马仕美白 去烟渍 清新口腔 牙膏75ML\\n\",\"FreeShopping\":\"0\",\"FreeTax\":\"1\",\"EndTime\":\"2016-05-06 13:59:59\"}");
		String ObjId = "223545f8-4b0d-4c7f-a97e-86d483546004";
		sendCommentBean.setObjId(ObjId);
		sendCommentBean.setObjType(1);
		sendCommentBean.setObjLogo("http://pc1.img.ymatou.com/G02/shangou/M05/24/59/CgvUBFcqvGmAP2m-AAcPDpP8dwE082-original_o.jpg");
		sendCommentBean.setPushTitle("这个好不好用！");
		return sendCommentBean;
	}

	/**
	 * 发送评论
	 * @param sendCommentBean
	 * @throws IOException
	 */
	public static void SendComment(SendCommentBean sendCommentBean) throws IOException {
		SendCommentCall sendCommentCall = new SendCommentCall();
		sendCommentCall.setData(sendCommentBean);
		sendCommentCall.callService();
	}

	/**
	 *
	 * @param sendCommentBean
	 * @param isNew  是否拉去新消息  No历史消息  yes新消息
	 * @param count  获取消息的个数
	 * @param LongTime  传入时间
	 * @return
	 */
	public static List<CommentDto> GetCommentListByTimeline(SendCommentBean sendCommentBean, String isNew, int count, Long LongTime) throws IOException {
		GetCommentListByTimelineBean getCommentListByTimeline = new GetCommentListByTimelineBean();
		GetCommentListByTimelineCall getCommentListByTimelineCall = new GetCommentListByTimelineCall();
		getCommentListByTimeline.setUserId(sendCommentBean.getToUserId());
		getCommentListByTimeline.setLongTimeline(LongTime);
		getCommentListByTimeline.setCount(count);
		getCommentListByTimeline.setIsNew(isNew);
		getCommentListByTimelineCall.setData(getCommentListByTimeline);
		getCommentListByTimelineCall.callService();
		List<CommentDto> commentDtoList = getCommentListByTimelineCall.getCommentDtoList();
		return commentDtoList;
	}

	/**
	 * 获取消息未读数
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	public static int GetMessageCount(int userId) throws IOException {
		GetMessageCountBean getMessageCountBean = new GetMessageCountBean();
		getMessageCountBean.setUserId(userId);
		GetMessageCountCall getMessageCountCall = new GetMessageCountCall();
		getMessageCountCall.setData(getMessageCountBean);
		getMessageCountCall.callService();
		int unReadCount = Integer.parseInt(getMessageCountCall.getString("Result"));
		return unReadCount;
	}

	/**
	 * 获取消息未读数，全球优选
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	public static int GetGlobalMessageCount(int userId) throws IOException {
		GetMessageCountBean getMessageCountBean = new GetMessageCountBean();
		getMessageCountBean.setUserId(userId);
		GetGlobalMessageCountCall getMessageCountCall = new GetGlobalMessageCountCall();
		getMessageCountCall.setData(getMessageCountBean);
		getMessageCountCall.callService();
		int unReadCount = Integer.parseInt(getMessageCountCall.getString("Result"));
		return unReadCount;
	}

	/**
	 * 初始化用户获取消息和会话信息bean
	 * @param fromUserId
	 * @param toUserId
	 * @return
	 */
	public static GetMessageListAndSessionByTimelineBean initGetMessageListAndSessionByTimelineBean(int fromUserId,int toUserId){
        GetMessageListAndSessionByTimelineBean getMessageListBean = new GetMessageListAndSessionByTimelineBean();
        getMessageListBean.setCsId(fromUserId);
        getMessageListBean.setFromUserId(fromUserId);
        getMessageListBean.setIsNew(MessageIsNewType.Yes);
        getMessageListBean.setIsNotClearUnReadCount(true);
        getMessageListBean.setMessageCount(100);
        getMessageListBean.setToUserId(toUserId);
        getMessageListBean.setLongTimeline(YMTDateUtil.getBeforeOrNextDate(-1).getTime());
        return getMessageListBean;
    }

	public static GetCSMessageListByTimelineBean initGetCSMessageListByTimeline(int CsId,int toUserId){
		GetCSMessageListByTimelineBean getMessageListBean = new GetCSMessageListByTimelineBean();
		getMessageListBean.setCsId(CsId);
		getMessageListBean.setFromUserId(CsId);
		getMessageListBean.setIsNew(MessageIsNewType.Yes);
		getMessageListBean.setIsNotClearUnReadCount(true);
		getMessageListBean.setMessageCount(10);
		getMessageListBean.setToUserId(toUserId);
		getMessageListBean.setLongTimeline(YMTDateUtil.getBeforeOrNextDate(-1).getTime());
		return getMessageListBean;
	}

	/**
	 * 获取会话消息
	 * @param getMessageListBean
	 * @return
	 * @throws IOException
	 */
	public static JsonObject GetMessageListAndSessionByTimeline(GetMessageListAndSessionByTimelineBean getMessageListBean) throws IOException {
		GetMessageListAndSessionByTimelineCall getmessagelistCall = new GetMessageListAndSessionByTimelineCall();
		getmessagelistCall.setData(getMessageListBean);
		getmessagelistCall.callService();
		return getmessagelistCall.getJsonObject();
	}


}
